package com.kx.face.service.impl;

import com.baidu.aip.face.AipFace;
import com.kx.base.util.FileBase64Util;
import com.kx.face.mapper.RequestLogMapper;
import com.kx.face.pojo.RequestLog;
import com.kx.face.service.FaceService;
import com.kx.face.util.FaceUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.image.BufferedImage;
import java.util.HashMap;

/**
 * @Author Sun
 * @Date 2021/7/20 23:15
 */
@Service
public class BaiduFaceServiceImpl implements FaceService {
    @Autowired
    private AipFace aipFace;

    @Resource
    private RequestLogMapper requestLogMapper;

    @Override
    public boolean addFace(Long userId, String imageBase64, Integer serviceId) {
        RequestLog requestLog = new RequestLog();
        requestLog.setActionId(1);
        requestLog.setServiceId(serviceId);
        requestLogMapper.insertSelective(requestLog);

        //base64->bufferedimage
        BufferedImage bufferedImage= FileBase64Util.base642Image(imageBase64);
        bufferedImage= FaceUtil.haveFace(bufferedImage);
        if(bufferedImage==null){
            //入参图片无脸
            //就不要去调用第三方的云服务，浪费QPS
            return false;
        }

        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("user_info", "我的人脸库");
        options.put("quality_control", "NORMAL");
        options.put("liveness_control", "LOW");
        options.put("action_type", "REPLACE");

        String imageType = "BASE64";
        String groupId = "face_group";
        String uId = userId + "";

        // 人脸注册
        JSONObject res = aipFace.addUser(imageBase64, imageType, groupId, uId, options);
        int error_code = res.getBigInteger("error_code").intValue();
        if(error_code == 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean delFace(Long userId, Integer serviceId) {
        RequestLog requestLog = new RequestLog();
        requestLog.setActionId(2);
        requestLog.setServiceId(serviceId);
        requestLogMapper.insertSelective(requestLog);

        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();

        String uid = userId + "";
        String groupId = "face_group";
        String faceToken = null;

        // 获取用户人脸列表
        JSONObject res = aipFace.faceGetlist(uid, groupId, options);
        if (res.getBigInteger("error_code").intValue() != 0) {
            return false;
        }
        JSONArray faceList = res.getJSONObject("result").getJSONArray("face_list");
        if (faceList != null && faceList.length() > 0) {
            faceToken = faceList.getJSONObject(0).getString("face_token");
        } else {
            return false;
        }

        // 传入可选参数调用接口
        options = new HashMap<String, String>();

        // 人脸删除
        res = aipFace.faceDelete(uid, groupId, faceToken, options);
        if (res.getBigInteger("error_code").intValue() == 0) {
            //成功
            return true;
        } else {
            //失败
            return false;
        }
    }

    @Override
    public Long search(String imageBase64, Integer serviceId) {
        RequestLog requestLog = new RequestLog();
        requestLog.setActionId(2);
        requestLog.setServiceId(serviceId);
        requestLogMapper.insertSelective(requestLog);

        //人脸搜索
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("match_threshold", "70");
        options.put("quality_control", "NORMAL");
        options.put("liveness_control", "LOW");

        String image = imageBase64;
        String imageType = "BASE64";
        String groupIdList = "face_group";

        // 人脸搜索
        JSONObject res = aipFace.search(image, imageType, groupIdList, options);
        if (res.getBigInteger("error_code").intValue() == 0) {
            //成功
            res = res.getJSONObject("result");
            JSONArray userList = res.getJSONArray("user_list");
            if (userList != null && userList.length() > 0) {
                String userId = userList.getJSONObject(0).getString("user_id");
                return Long.parseLong(userId);
            } else {
                return null;
            }
        } else {
            //失败
            return null;
        }
    }
}
